﻿using System;
using System.Collections.Generic;

namespace AIC_Notification
{
    class RunStatusReport: BaseReport
    {
        public RunStatusReport()
        {
            SubjectID = String.Format("AIC Notification Run - {0:G}", Global.Now());
            FileName = "Report.htm";
        }

        public override void SendReportAsEmail()
        {
            // Send report
            string[] SystemAdmins = TeacherDB.GetSystemAdmin();
            foreach (var SystemAdmin in SystemAdmins)
                ccRecipients.Add(SystemAdmin);

            TimeSpan TimeLapped = Statistics.EndTime - Statistics.StartTime;

            string ProgramTypeString = "SOF";
            switch (Statistics.ApplicationType)
            {
                case ApplicationType.sof:
                    ProgramTypeString = "SOF";
                    break;
                case ApplicationType.guidance:
                    ProgramTypeString = "Guidance";
                    break;
                default:
                    ProgramTypeString = "Error";
                    break;
            }

            string RunTypeString = "Full Read";
            switch (Statistics.RunType)
            {
                case RunType.full:
                    RunTypeString = "Full Read";
                    break;
                case RunType.update:
                    RunTypeString = "Partial Update";
                    break;
                case RunType.cache:
                    RunTypeString = "Cache Only";
                    break;
                case RunType.skip:
                    RunTypeString = "Skip";
                    break;
                default:
                    RunTypeString = "Error";
                    break;
            }

            if (Statistics.RunType == RunType.full || Statistics.RunType == RunType.update)
            {
                string HTMLBody = "";
                List<string> Attachments = new List<string>();

                HTMLBody += "<html><body>";

                HTMLBody += String.Format("<div>{0}: {1}</div>", "Program Type", ProgramTypeString);
                HTMLBody += String.Format("<div>{0}: {1}</div>", "Run Type", RunTypeString);
                HTMLBody += String.Format("<div>{0}: {1}</div>", "Start Time", Statistics.StartTime);
                if (Statistics.TotalBoard != 0 && Statistics.ProcessingBoard == Statistics.TotalBoard) // Success
                {
                    HTMLBody += String.Format("<div>{0}: {1}</div>", "End Time", Statistics.EndTime);
                    HTMLBody += String.Format("<div>{0}: {1:c}</div>", "Total Run Time", TimeLapped);
                    HTMLBody += String.Format("<div>{0}: {1:0.00}</div>", "Time Per Board", TimeLapped.TotalSeconds / Statistics.TotalBoard);
                }
                else // Failure
                {
                    // Attachments.Add(Global.LogPathFile); // Disable Log file until we have a zip
                }
                HTMLBody += String.Format("<div>{0}: {1}</div>", "Total Number of Boards", Statistics.TotalBoard);
                HTMLBody += String.Format("<div>{0}: {1}</div>", "Board Processed", Statistics.ProcessingBoard);

                HTMLBody += "</body></html>";

                Attachments.Add(Global.ErrorPathFile);

                Global.SysUtil.SendHTML(toRecipients, null, SubjectID, HTMLBody, Attachments);
            }
        }
    }
}
